<?php
declare (strict_types=1);

namespace app\admin\validate\admin;

use think\Validate;

class Role extends BaseRole
{

    private array $allowedModifyFields = ['status'];

    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [


    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [

    ];

    protected $scene = [
        'index' => ['*'],
        'add' => ['*'],
        'update' => ['*'],
    ];


    public function sceneDelete()
    {
        return $this->only(['ids'])
            ->rule([
                'ids' => 'require|array',
                'ids.*' => 'number|gt:0',
            ])->message([
                'ids.require' => 'ID 不能为空',
                'ids.array' => 'ID 必须是数组格式',
                'ids.*.number' => 'ID 必须是数字',
                'ids.*.gt' => 'ID 无效',
            ]);
    }

    public function sceneModify(): Role
    {
        return $this->only(['id', 'field', 'value'])
            ->rule([
                'id' => 'require|integer|gt:0',
                'field' => 'require|string|in:' . implode(',', $this->allowedModifyFields),
            ])->message([
                'id.require' => '用户ID不能为空',
                'id.integer' => '用户ID必须是整数',
                'id.gt' => '用户ID无效',
                'field.require' => '要修改的字段名不能为空',
                'field.in' => '不允许修改该字段',
            ]);
    }
}
